import Ember from 'ember';
import BaseBusiness from '../base-business';
import Pagination from '../pagination';

export default BaseBusiness.extend(Pagination, {
    dataService: Ember.inject.service("date-service"),
    header_title: '老人动态及预警信息',
    model() {
        return {};
    },
    buildQueryParams: function() {
        var params = this.pagiParamsSet();
        var curController = this.getCurrentController();
        let chooseTabFlag = curController.get('chooseTabFlag');
        var filter = {};
        var sort;
        console.log("warning-management1111", this.get("dataService"));
        if (curController) {
            if(curController.get('chooseTabFlag')){
              if(chooseTabFlag == "chooseTabOne"){
                filter = $.extend({}, filter, {'reason@$like':"呼叫"});
              }else if(chooseTabFlag == "chooseTabTwo"){
                filter = $.extend({}, filter, {'reason@$not---1':"老人摔倒按键自动呼叫"});
                filter = $.extend({}, filter, {'reason@$not---2':"老人主动按键呼叫"});
                filter = $.extend({}, filter, {'reason@$not---3':"华米按键呼叫"});
                filter = $.extend({}, filter, {'reason@$not---4':"按键呼叫"});
                filter = $.extend({}, filter, {'reason@$not---5':"深睡眠"});
                filter = $.extend({}, filter, {'reason@$not---6':"浅睡眠"});
                filter = $.extend({}, filter, {'reason@$not---7':"醒着"});
              }else{
              }
            }
            if(curController.get('warningCustomer')){
              if(chooseTabFlag == "chooseTabThree"){
                filter = $.extend({}, filter, {'[customer][id]':curController.get('warningCustomer.id')});
              }else{
                filter = $.extend({}, filter, {'[caller][id]':curController.get('warningCustomer.id')});
              }
            }
            //时间条件搜索
            var queryCondition = curController.get('queryCondition');
            if (queryCondition) {
                var compareDate = null;
                if (queryCondition === "today") {
                    compareDate = this.get("dataService").getTodayTimestamp();
                }
                if (queryCondition === "seven") {
                    compareDate = this.get("dataService").getDaysBeforeTimestamp(7);
                }
                if (queryCondition === "thirty") {
                    compareDate = this.get("dataService").getDaysBeforeTimestamp(30);
                }
                if(chooseTabFlag == "chooseTabThree"){
                  filter = $.extend({}, filter, {
                      'createDate@$gte': compareDate
                  });
                }else{
                  filter = $.extend({}, filter, {
                      'callTime@$gte': compareDate
                  });
                }
                console.log("compareDate is", compareDate);
            }
            //自定义日期搜索
            var conBeginDate = curController.get("beginDate");
            console.log("++++conBeginDate+++++", conBeginDate);
            var conEndDate = curController.get("endDate");
            console.log("++++conEndDate+++++", conEndDate);
            if (conBeginDate && conEndDate) {
                var beginCompare = this.get("dataService").getFirstSecondStampOfDay(conBeginDate);
                console.log("beginCompare is", beginCompare);
                var endCompare = this.get("dataService").getLastSecondStampOfDay(conEndDate);
                console.log("endCompare is", endCompare);
                if(chooseTabFlag == "chooseTabThree"){
                  filter = $.extend({}, filter, {
                      'createDate@$gte': beginCompare
                  }, {
                      'createDate@$lte': endCompare
                  });
                }else{
                  filter = $.extend({}, filter, {
                      'callTime@$gte': beginCompare
                  }, {
                      'callTime@$lte': endCompare
                  });
                }
            }
        }
        params.filter = filter;
        if(chooseTabFlag == "chooseTabThree"){
          sort = {
              '[createDate]': 'desc',
          };
        }else{
          sort = {
              '[callTime]': 'desc',
          };
        }
        params.sort = sort;
        return params;
    },
    doQuery: function() {
        var params = this.buildQueryParams();
        let warningList;
        var curController = this.getCurrentController();
        let chooseTabFlag = curController.get('chooseTabFlag');
        if(chooseTabFlag == "chooseTabThree"){
          warningList = this.findPaged('sleepmessage', params, function(warningList) {});
          console.log("warningList in three:",warningList);
        }else{
          warningList = this.findPaged('hbeaconwarning', params, function(warningList) {});
        }
        curController.set("warningList", warningList);
    },
    setupController: function(controller, model) {
        controller.set("chooseTabFlag","chooseTabOne");
        this.doQuery();
        this._super(controller, model);
        let filterCustomer;
        filterCustomer = $.extend({}, filterCustomer, {
            '[customerStatus][typecode@$like]@$or1---1': 'customerStatusIn'
        });
        filterCustomer = $.extend({}, filterCustomer, {
            '[customerStatus][typecode@$like]@$or1---2': 'customerStatusTry'
        });
        //查询所有的客户信息
        this.store.query('customer', {filterCustomer}).then(function(customerList) {
            customerList.forEach(function(customer) {
                customer.set('sortName', pinyinUtil.getFirstLetter(customer.get("name")));
            });
            controller.set('customerList', customerList);
        });
    }
});
